//
//  PeopleServiceHomeVC.m
//  Cunli
//
//  Created by ZhanyaaLi on 15/12/4.
//  Copyright © 2015年 lilubin. All rights reserved.
//

#import "PeopleServiceHomeVC.h"
#import "PeopleServiceCell.h"
#import "LBBtnModel.h"
#import "PeopleServiceCell.h"
#import "PeopleServiceCell1.h"
#import "TongZhiViewController.h"
#import "CunWuListViewController.h"
#import "MediatorViewController.h"
#import "LawyerViewController.h"
#import "FamilyCallViewController.h"
#import "ZhiShiViewController.h"
#import "Config.h"
#import "HMBannerView.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "NewDetailController.h"
#import "ActivityWebVC.h"
#import "UIColor+expanded.h"
#import "VillageRuleViewController.h"
#import "VillageIntroduceVC.h"
#import "DeviceInfoClass.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface PeopleServiceHomeVC () <UITableViewDelegate, UITableViewDataSource, HMBannerViewDelegate>
{
    NSArray *_modelArray;
    NSArray *_modelArray2;
    NSArray *_bannerDatasArray;  //存放banner的数据，字典形式的数组
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HMBannerView *bannerView;

@end

@implementation PeopleServiceHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initGlobleParams];
    [self createTableView];
    [self loadBannerData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)createTableView
{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
//    _tableView.sectionFooterHeight = 1;
//    _tableView.sectionHeaderHeight = 1;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xf6f6f6"];
    
}

- (void)initGlobleParams
{
    LBBtnModel *model1 = [[LBBtnModel alloc] initWithImgName:@"infoNews_icon" labelTitle:@"通知通告" btnType:LBBtnTypeNotification];
    LBBtnModel *model2 = [[LBBtnModel alloc] initWithImgName:@"villageAffaris_icon" labelTitle:@"村务公开" btnType:LBBtnTypeVillageAffaris];
    LBBtnModel *model3 = [[LBBtnModel alloc] initWithImgName:@"villageRules_icon" labelTitle:@"村规民约" btnType:LBBtnTypeVillageRules];
    LBBtnModel *model6 = [[LBBtnModel alloc] initWithImgName:@"villageintroduce_icon" labelTitle:@"本村介绍" btnType:LBBtnTypeVillageIntro];
    _modelArray = [NSArray arrayWithObjects:model1, model2, model3, model6, nil];
    
    LBBtnModel *model4 = [[LBBtnModel alloc] initWithImgName:@"mediatorCall_icon" labelTitle:@"人民调解员" btnType:LBBtnTypeCallMediator];
    LBBtnModel *model5 = [[LBBtnModel alloc] initWithImgName:@"lawyerCall_icon" labelTitle:@"援助律师" btnType:LBBtnTypeCallLawyer];
    _modelArray2 = [NSArray arrayWithObjects:model4, model5, nil];
    
    
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *normalCell = [UITableViewCell new];
    normalCell.contentView.backgroundColor = [UIColor colorWithHexString:@"0xf6f6f6"];
    if (indexPath.row == 3) {
        PeopleServiceCell *cell = [[PeopleServiceCell alloc] init];
        cell.cellTitle = @"村务公开";
        cell.buttonsArray = _modelArray;
        cell.LBBtnClicked = ^(LBBtnType btnType){
            [self LBBtnAction:btnType];
        };
        return cell;
    }
    else if(indexPath.row == 5)
    {
        PeopleServiceCell *cell = [[PeopleServiceCell alloc] init];
        cell.cellTitle = @"在线服务";
        cell.buttonsArray = _modelArray2;
        cell.LBBtnClicked = ^(LBBtnType btnType){
            [self LBBtnAction:btnType];
        };
        return cell;
    }
    else if(indexPath.row == 1)
    {
        PeopleServiceCell1 *cell = [[NSBundle mainBundle] loadNibNamed:@"PeopleServiceCell1" owner:self options:nil].lastObject;
        cell.TapTouch = ^(int touchX){
            if (touchX < (kScreenWidth/2)) {
                NSLog(@"Touch Left");
                //点击左侧区域，执行对应方法
                [self LBBtnAction:LBBtnTypeFamilyCall];
            }
            else
            {
                NSLog(@"Touch Right");
                //点击右侧区域，执行相应方法
                [self LBBtnAction:LBBtnTypeKnowledge];
            }
        };
        return cell;
    }
    return normalCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        return ((kScreenWidth * 1.0 - 20)/ 3 * 2 + 23);
    }
    else if(indexPath.row == 5)
    {
        return ((kScreenWidth * 1.0 - 20)/3 + 23);
    }
    else if(indexPath.row == 1)
    {
        return 100;
    }
    return 10;
}

- (void)LBBtnAction:(LBBtnType)btnType
{
    UIViewController *specificVC;
    switch (btnType) {
        case LBBtnTypeNotification:
        {
            specificVC = [[TongZhiViewController alloc] init];
            break;
        }
        case LBBtnTypeVillageAffaris:
        {
            specificVC = [[CunWuListViewController alloc] init];
            break;
        }
        case LBBtnTypeVillageRules:
        {
            specificVC = [[VillageRuleViewController alloc] init];
            break;
        }
        case LBBtnTypeCallMediator:
        {
            specificVC = [[MediatorViewController alloc] init];
            break;
        }
        case LBBtnTypeCallLawyer:
        {
            specificVC = [[LawyerViewController alloc] init];
            break;
        }
        case LBBtnTypeFamilyCall:
        {
            specificVC = [[FamilyCallViewController alloc] init];
            break;
        }
        case LBBtnTypeKnowledge:
        {
            specificVC = [[ZhiShiViewController alloc] init];
            break;
        }
        case LBBtnTypeVillageIntro:
        {
            specificVC = [[VillageIntroduceVC alloc] init];
            break;
        }
        default:
            break;
    }
    [self pushToSpecificViewController:specificVC];
}

- (void)pushToSpecificViewController:(UIViewController *)vc
{
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)createHeaderView
{
    CGFloat headerHeight = 100;
    NSArray *heightArray = @[@106, @106, @125, @138];
    NSNumber *heightNumber = heightArray[[DeviceInfoClass getCurrentDeviceDetailVersion]];
    headerHeight = heightNumber.floatValue;

    if (_bannerDatasArray.count == 0) {
        headerHeight = 0;
    }
    /*
    else if(_bannerDatasArray.count == 1)
    {
        UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headerHeight)];
        NSDictionary *dic = _bannerDatasArray[0];
        [bgImgView setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
        self.tableView.tableHeaderView = bgImgView;
    }
     */
    else {
        
        if (_bannerView != nil)
        {
            [_bannerView reloadBannerWithData:_bannerDatasArray];
        }
        else
        {
            _bannerView = [[HMBannerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headerHeight) scrollDirection:ScrollDirectionLandscape images:_bannerDatasArray];
            [_bannerView setRollingDelayTime:4.0];
            [self.bannerView setLabelHidden:YES];
            [_bannerView setDelegate:self];
            [_bannerView setPageControlStyle:PageStyle_Right];
            [_bannerView startDownloadImage];
            //[self.m_BannerView showClose:YES];
        }
        
        self.tableView.tableHeaderView = _bannerView;
    }
}

- (void)loadBannerData
{
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:5];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:@"5" forKey:@"type"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    [CheckParams setObject:@"conv_index_banner" forKey:@"position"];
//    [CheckParams setObject:@"news_zw_banner" forKey:@"position"];

    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    _bannerDatasArray = [NSArray array];
    [MyDataService requestURL:@"ads.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"Convenient Banner datas:%@", result);
        if (![result[@"response"] isEqualToString:@"error"])
        {
            _bannerDatasArray = result[@"adslist"];
            [self createHeaderView];
        }
    } failureAction:^{
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];
}

#pragma mark HMBannerViewDelegate

- (void)imageCachedDidFinish:(HMBannerView *)bannerView
{
    [self.tableView addSubview:bannerView];
    [bannerView startRolling];
}

- (void)bannerView:(HMBannerView *)bannerView didSelectImageView:(NSInteger)index withData:(NSDictionary *)bannerData
{
    NSLog(@"TAP");
    if (bannerData != nil) {
        if ([bannerData[@"type"] integerValue] == 1)
        {
            NewDetailController *newDetailVC = [[NewDetailController alloc] init];
            newDetailVC.itemID = bannerData[@"atc_itemid"];
            newDetailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:newDetailVC animated:YES];
        }
        else if([bannerData[@"type"] integerValue] == 10)
        {
            [self jumpToActivity:bannerData[@"atc_itemid"] activityUrl:bannerData[@"imageurl"]];
        }
    }
}

- (void)jumpToActivity:(NSString *)activityId activityUrl:(NSString *)activityUrl
{
    ActivityWebVC *activityVC = [[ActivityWebVC alloc] init];
    activityVC.hidesBottomBarWhenPushed = YES;
    activityVC.activityId = activityId;
    activityVC.activityUrl = activityUrl;
    [self.navigationController pushViewController:activityVC animated:YES];
}



- (void)bannerViewdidClosed:(HMBannerView *)bannerView;
{
    if (bannerView.superview)
    {
        [bannerView removeFromSuperview];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (_bannerView != nil) {
        [_bannerView stopRolling];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_bannerView != nil) {
        [_bannerView startRolling];
    }
}


@end
